import axios from 'axios'
import {BASE_URL} from '../constants/api'
import {http} from "./http"

axios.defaults.headers['apiVersion'] = '3.0'
axios.defaults.headers.post['Content-Type'] = 'multipart/form-data'

const service = axios.create({
  baseURL: BASE_URL,
  timeout: 10000
})

service.interceptors.request.use(
  config => {
    config.method = 'post'
    config.headers['token'] = http.getToken()
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

service.interceptors.response.use(
  response => {
    return response.data
  },
  error => {
    if (!error.response) {
      error = { response : { data: { message: '上传失败' } } }
    }
    return Promise.reject(error.response.data)
  }
)

export default service
